const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            dataForm: { //longtext类型
                Training_Content: '',
                Party_Organization_Opinion: '',
                Parent_Party_Organization_Opinion: '',
                Record_Content: '',
                Training_Result: '',
                Party_Organization_Resolution: '',
                Part_Organization_Resolution: '',
                Remark: ''
            },
            personal: [],
            personal1:[],
            organization: [],
            Party_Organization: [],
            index: 0,
            stepData: [],
            party_member_application:[],
            personal_information:[]
        };
    },
    mounted() {
        let ls_URL = window.location.href;
        let index = getQueryObject(ls_URL, "index") ;
        this.index = index!='0' ? Number(index) + 1:'0';
        this.getPersonal();
    },
    methods: {
        changePersonal(val){
            let obj = this.personal.filter(i => i.ID_Card_Number == val)[0];
            if (obj) {
                this.dataForm.Contact_Phone = obj.Contact_Phone;
            }
        },
        getData(nodeKey) {//查询
            let ls_URL = window.location.href;
            this.index = getQueryObject(ls_URL, "index");
            if (this.action == 'Modify') {
                if (this.index == '0') {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        method: "post",
                        data: {
                            Data: 'party_member_application',
                            Condition: `Application_SerialNumber='${nodeKey}'`,
                        },
                        success: (res) => {
                            this.dataForm = {...this.dataForm,...res[0]} ;
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                    });
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        method: "post",
                        data: {
                            Data: 'party_member_log',
                            Condition: `Application_SerialNumber='${nodeKey}' AND Operation_Step_SerialNumber='PMOS00000000002'`,
                        },
                        success: (res) => {
                            this.dataForm.Attachment_URL = res[0].Attachment_URL;
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                    });
                } else {
                    let Party_Member_Log_SerialNumber = getQueryObject(ls_URL, "Party_Member_Log_SerialNumber");
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        method: "post",
                        data: {
                            Data: 'party_member_log',
                            Condition: `Party_Member_Log_SerialNumber='${Party_Member_Log_SerialNumber}'`,
                        },
                        success: (res) => {
                            this.dataForm = res[0];
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                    });
                }
            }

        },
        getPersonal() {
            let ls_URL = window.location.href;
            let ID_Card_Number = getQueryObject(ls_URL, "ID_Card_Number");
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 'f_personal_information4',
                    Condition: '',
                    ExtensionCondition: '',
                },
                success: (data) => {
                    this.personal = data; // 直接使用 this
                    console.log(data);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            })
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'party_member_application',
                    Condition: `Application_SerialNumber='${this.nodeKey}'`,
                },
                success: (res) => {
                    this.party_member_application = res;
                    console.log(res);
                    console.log();
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                        method: "post",
                        data: {
                            SQLSerialNumber: 'f_party_member_information',
                            Condition: '',
                            ExtensionCondition: utf8ToBase64(`AND party_member_information.Party_Organization_SerialNumber='${res[0].Party_Organization_SerialNumber}'`),
                        },
                        success: (data) => {
                            this.personal1 = data; // 直接使用 this
                            console.log(data);
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest)
                        },
                    })
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'personal_information',
                    Condition: `ID_Card_Number='${ID_Card_Number}'`,
                },
                success: (res) => {
                    this.personal_information = res;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                }
            })
            
            // $.ajax({
            //     headers: requestHeaders(),
            //     url: `${mh}/generalBusiness/generalQueryByTableName`,
            //     type: "POST",
            //     data: {
            //         Data: 'personal_information',
            //         Condition: "Row_Status='0'",
            //     },
            //     success: (data) => {
            //         this.personal = data; // 直接使用 this
            //     },
            //     complete: (XMLHttpRequest) => {
            //         requestPretreatment(XMLHttpRequest);
            //     },
            //     dataType: "json",
            // });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'party_organization',
                    NodeCode: '',
                },
                success: (data) => {
                    this.organization = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "post",
                data: {
                    Data: 'party_member_operation_step',
                    Condition: "Row_Status='0' ORDER BY Operation_Step_Sequence + 0;",
                },
                success: (data) => {
                    this.stepData = data;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
        },
        success(res) {
            this.dataForm.Attachment_Path = res.Path;
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                let ls_URL = window.location.href;
                const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
                const Step_SerialNumber = getQueryObject(ls_URL, "Step_SerialNumber");   // 获取 action 参数
                const ID_Card_Number = getQueryObject(ls_URL, "ID_Card_Number");
                const Party_Organization_SerialNumber = getQueryObject(ls_URL, "Party_Organization_SerialNumber");
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));
                let ls_MainTableName = lJSONObject_Data_Options.TableName;
                let ls_DataForm = lJSONObject_Data_Options.form;
                if (this.index == 0 && action == 'Add') {
                    this[ls_DataForm].Apply_Date = getYMDDate();
                    this[ls_DataForm].Row_Status = '1';
                    this[ls_DataForm].Children = {
                        party_member_log: [{
                            Operation_Step_SerialNumber: this.stepData[0].Operation_Step_SerialNumber,
                            Training_Content: '',
                            Party_Organization_Opinion: '',
                            Parent_Party_Organization_Opinion: '',
                            Record_Content: '',
                            Training_Result: '',
                            Party_Organization_Resolution: '',
                            Part_Organization_Resolution: '',
                            Attachment_URL: this[ls_DataForm].Attachment_URL,
                            Attachment_Path: this[ls_DataForm].Attachment_Path,
                        }],
                    }
                } else {
                    this[ls_DataForm].Application_SerialNumber = this.nodeKey;
                    this[ls_DataForm].Operation_Step_SerialNumber = Step_SerialNumber;
                }
                //没有子表下面内容就会报错，try/catch抛出异常
                try {
                    //可能涉及多个子表
                    document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                        let lJSONObject_ChildData_Options = new Object();
                        lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                        let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                        let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
                        // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
                        //建立子表结构体
                        let JSONArray_ChildRecord = new Array();
                        JSONArray_ChildRecord = this[ls_ChildDataForm];
                        if (!this[ls_DataForm].Children) {
                            this[ls_DataForm].Children = new Object();
                        }
                        this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
                    })
                } catch (error) {

                }
                //建立新增提交结构
                let lJSONObject_DataSet = new Object();
                if (action == 'Add') {
                    if (this.nodeKey) {
                        this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
                    }
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(this[ls_DataForm]);
                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
                    lJSONObject_DataSet[action] = lJSONObject_Table;
                    if (this.index == 3) {
                        this.party_member_application[0].Row_Status = '2';
                        lJSONObject_DataSet['Modify'] = {
                            party_member_application:this.party_member_application
                        }
                    } else if (this.index == 7) {
                        this.party_member_application[0].Row_Status = '3';
                        lJSONObject_DataSet['Modify'] = {
                            party_member_application:this.party_member_application
                        }
                    } else if (this.index == 17) {
                        this.party_member_application[0].Row_Status = '4';
                        lJSONObject_DataSet['Modify'] = {
                            party_member_application:this.party_member_application
                        }
                    } else if (this.index==19) { //在党员表新增一条记录
                        this.personal_information[0].Political_Affiliation = '中共预备党员';
                        lJSONObject_DataSet['Add']['party_member_information'] = [{
                            ID_Card_Number,
                            Joining_Date:this.dataForm.Determine_Date,
                            Conformation_Date:this.dataForm.Determine_Date,
                            Party_Organization_SerialNumber,
                        }]
                        lJSONObject_DataSet['Modify'] = { personal_information:this.personal_information }
                    }else if (this.index==24) {
                        this.personal_information[0].Political_Affiliation = '中共党员';
                        this.party_member_application[0].Row_Status = '5';
                        lJSONObject_DataSet['Modify'] = {
                            party_member_application:this.party_member_application,
                            personal_information:this.personal_information
                        }
                    }
                } else if (action == 'Modify') {
                   
                    const { add, modify } = this.deal(this[ls_DataForm]);
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(modify);

                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

                    lJSONObject_DataSet[action] = lJSONObject_Table;
                    lJSONObject_DataSet['Add'] = new Object();
                    if (add != null) {
                        let lJSONArray_Record_Add = new Array();
                        lJSONArray_Record_Add.push(add);

                        let lJSONObject_Table_Add = new Object();
                        lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

                        lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
                    }
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                if (!lJSONObject_DataSet['Add']) {
                    lJSONObject_DataSet['Add'] = new Object();
                }
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSave",
                    Operation_IP: getIpAddress()
                }]
                // return
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        window.history.go(-1);
                    }
                    this.loading = false;
                })
            }
            // $.ajax({
            //     headers: requestHeaders(),
            //     url: `${mh}/generalBusiness/generalSave`,
            //     method: "post",
            //     data: {
            //         Data: JSON.stringify(lJSONObject_DataSet),
            //     },
            //     success: (res) => {
            //         if (res.Status == 'success') {
            //             window.history.go(-1);
            //         }
            //     },
            //     complete: (XMLHttpRequest) => {
            //         requestPretreatment(XMLHttpRequest);
            //     },
            // });
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-select', ylSelect);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
